home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / DroneZone / DZUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.5 KB  |  191 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        DZUtils.c
  3.  *
  4.  *    Copyright © 1996 Apple Computer, Inc.
  5.  */
  6.  
  7. #include <assert.h>
  8.  
  9. #include <Resources.h>
  10. #include <Types.h>
  11.  
  12. #include <QD3D.h>
  13. #include <QD3DGroup.h>
  14. #include <QD3DIO.h>
  15. #include <QD3DStorage.h>
  16.  
  17. #include "DZUtils.h"
  18.  
  19.  
  20. /*******************************************************************************
  21.  *    CheckVersionNumber
  22.  *
  23.  *    Returns true if the given version number is compatible with (i. e not older
  24.  *    than) version inMajor.inMinor.inBug.
  25.  ******************************************************************************/
  26. Boolean CheckVersionNumber(
  27.     const NumVersion*        inVersion,
  28.     UInt8                    inMajor,
  29.     UInt8                    inMinor,
  30.     UInt8                    inBug)
  31. {
  32.     if (inVersion->majorRev != inMajor)
  33.     {
  34.         return inVersion->majorRev > inMajor;
  35.     }
  36.     else
  37.     {
  38.         return inVersion->minorAndBugRev >= inMinor << 4 | inBug;
  39.     }
  40. }
  41.  
  42.  
  43. /* =============================================================================
  44.  *        Get3DMFResource (external)
  45.  *
  46.  *    Gets the '3DMF' resource with the given ID, and reads it using QD3D, and
  47.  *    returns a reference to anything drawable in it.  NULL is returned if a
  48.  *    problem occurs.
  49.  * ========================================================================== */
  50. TQ3Object Get3DMFResource(
  51.     short                inResourceID)
  52. {
  53.     Handle                resource;
  54.     TQ3Object            result;
  55.     TQ3Object            object;
  56.     TQ3Object            group;
  57.     TQ3StorageObject    storage;
  58.     TQ3FileObject        file;
  59.     TQ3FileMode            mode;
  60.     
  61.     resource    = NULL;
  62.     result        = NULL;
  63.     object        = NULL;
  64.     group        = NULL;
  65.     storage        = NULL;
  66.     file        = NULL;
  67.     
  68.     // Grab the resource
  69.     resource = GetResource('3DMF', inResourceID);
  70.     if (resource == NULL || ResError() != noErr)  goto bail;
  71.     
  72.     // Set up for reading
  73.     storage = Q3HandleStorage_New(resource, GetHandleSize(resource));
  74.     if (storage == NULL)  goto bail;
  75.     
  76.     file = Q3File_New();
  77.     if (file == NULL)  goto bail;
  78.     
  79.     if (Q3File_SetStorage(file, storage) != kQ3Success)  goto bail;
  80.     
  81.     if (Q3File_OpenRead(file, &mode) != kQ3Success)  goto bail;
  82.     
  83.     // Read the objects
  84.     group = NULL;
  85.     
  86.     while (!Q3File_IsEndOfFile(file))
  87.     {
  88.         object = Q3File_ReadObject(file);
  89.         if (object != NULL)
  90.         {
  91.             if (Q3Object_IsDrawable(object))
  92.             {
  93.                 if (result == NULL)
  94.                 {
  95.                     // This is the first object
  96.                     result = Q3Shared_GetReference(object);
  97.                 }
  98.                 else
  99.                 {
  100.                     // Make sure we have a group to add it to
  101.                     if (group == NULL)
  102.                     {
  103.                         // Create the new group
  104.                         group = Q3DisplayGroup_New();
  105.                         Q3Group_AddObject(group, result);
  106.                         
  107.                         Q3Object_Dispose(result);
  108.                         result = Q3Shared_GetReference(group);
  109.                     }
  110.                     
  111.                     // Add it to the group
  112.                     assert(group == result);
  113.                     Q3Group_AddObject(result, object);
  114.                 }
  115.             }
  116.             
  117.             Q3Object_Dispose(object);
  118.             object = NULL;
  119.         }
  120.     }
  121.     
  122.     // Get rid of the group reference
  123.     if (group != NULL)
  124.     {
  125.         Q3Object_Dispose(group);
  126.         group = NULL;
  127.     }
  128.     
  129.     // Finish reading
  130.     Q3File_Close(file);
  131.     
  132.     Q3Object_Dispose(file);
  133.     file = NULL;
  134.     
  135.     Q3Object_Dispose(storage);
  136.     storage = NULL;
  137.     
  138.     // Release the resource
  139.     ReleaseResource(resource);
  140.     
  141.     return result;
  142.     
  143.     // Error exit
  144.     bail:
  145.     
  146.     if (resource != NULL)
  147.     {
  148.         ReleaseResource(resource);
  149.         resource = NULL;
  150.     }
  151.     
  152.     if (result != NULL)
  153.     {
  154.         Q3Object_Dispose(result);
  155.         result = NULL;
  156.     }
  157.     
  158.     if (object != NULL)
  159.     {
  160.         Q3Object_Dispose(object);
  161.         object = NULL;
  162.     }
  163.     
  164.     if (group != NULL)
  165.     {
  166.         Q3Object_Dispose(group);
  167.         group = NULL;
  168.     }
  169.     
  170.     if (result != NULL)
  171.     {
  172.         Q3Object_Dispose(result);
  173.         result = NULL;
  174.     }
  175.     
  176.     if (storage != NULL)
  177.     {
  178.         Q3Object_Dispose(storage);
  179.         storage = NULL;
  180.     }
  181.     
  182.     if (file != NULL)
  183.     {
  184.         Q3Object_Dispose(file);
  185.         file = NULL;
  186.     }
  187.     
  188.     return NULL;
  189. }
  190.  
  191.